package org.example.trigger.http;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.example.api.IActivityManageApiService;
import org.example.api.model.dto.ActivityDTO;
import org.example.api.model.dto.ActivityStrategyPrizeDTO;
import org.example.api.model.dto.PrizeDTO;
import org.example.api.model.response.Response;
import org.example.domain.activity_manage.model.entity.ActivityEntity;
import org.example.domain.activity_manage.model.entity.ActivityStrategyPrizeEntity;
import org.example.domain.activity_manage.model.entity.PrizeEntity;
import org.example.domain.activity_manage.service.IActivityService;
import org.example.domain.activity_manage.service.IActivityStrategyPrizeService;
import org.example.domain.activity_manage.service.IPreloadService;
import org.example.domain.activity_manage.service.IPrizeService;
import org.example.types.common.ResponseCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author lxc18
 * @date 2025/3/24 15:49
 * @description ActivityManageController
 */
@RestController
@Slf4j
@RequestMapping("/api/${app.config.api-version}/activity-manage/")
@Tag(name = "活动运营管理")
public class ActivityManageController implements IActivityManageApiService {
    @Resource
    private IActivityService activityService;
    @Resource
    private IPrizeService prizeService;
    @Resource
    private IActivityStrategyPrizeService activityStrategyPrizeService;

    @Resource
    private IPreloadService preloadService;
    @Override
    @Operation(description = "创建活动")
    @RequestMapping(value = "/create-activity", method = RequestMethod.POST)
    public Response<Boolean> createActivity(@RequestParam String activityName,
                                            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
                                            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        try {
            // 创建活动
            List<ActivityEntity> entities = activityService.queryActivityByName(activityName);
            for (ActivityEntity entity : entities) {
                if(entity.getActivityName().equals(activityName)
                        && entity.getStartTime().equals(startTime)
                        && entity.getEndTime().equals(endTime)){
                            log.error("创建活动{}失败，活动已存在",activityName);
                            return Response.<Boolean>builder()
                                .code(ResponseCode.ACTIVITY_MANAGE_CREATE_ACTIVITY_FAIL.getCode())
                                .info(ResponseCode.ACTIVITY_MANAGE_CREATE_ACTIVITY_FAIL.getInfo())
                                .data(null)
                                .build();
                        }
            }
            ActivityEntity activityEntity = new ActivityEntity();
            activityEntity.setActivityName(activityName);
            activityEntity.setStartTime(startTime);
            activityEntity.setEndTime(endTime);
            boolean result = activityService.createActivity(activityEntity);

            // 初始化默认策略
            List<ActivityEntity> activityEntities = activityService.queryActivityByName(activityName);
            for (ActivityEntity activityEntity1 : activityEntities) {
                if(activityEntity1.getActivityName().equals(activityName)
                        && activityEntity1.getStartTime().equals(startTime)
                        && activityEntity1.getEndTime().equals(endTime)){
                    boolean b = activityStrategyPrizeService.initDefaultCreateStrategy(activityEntity1.getActivityId());
                    if(!b){
                        log.error("创建活动{}的策略失败",activityName);
                        return Response.<Boolean>builder()
                              .code(ResponseCode.ACTIVITY_MANAGE_CREATE_ACTIVITY_FAIL.getCode())
                              .info(ResponseCode.ACTIVITY_MANAGE_CREATE_ACTIVITY_FAIL.getInfo())
                              .data(null)
                              .build();

                    }
                    log.info("创建活动{}的策略成功",activityName);
                    break;
                }
            }

            log.info("创建活动{}成功",activityName);
            return Response.<Boolean>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(result)
                    .build();
        } catch (Exception e) {
            log.error("创建活动{}，起始时间{}，截止时间{}，失败", activityName, startTime, endTime);
            return Response.<Boolean>builder()
                    .code(ResponseCode.ACTIVITY_MANAGE_CREATE_ACTIVITY_FAIL.getCode())
                    .info(ResponseCode.ACTIVITY_MANAGE_CREATE_ACTIVITY_FAIL.getInfo())
                    .data(null)
                    .build();
        }
    }

    @Override
    @Operation(description = "更新活动信息")
    @RequestMapping(value = "/update-activity", method = RequestMethod.POST)
    public Response<Boolean> updateActivity(@RequestParam Long activityId,
                                            @RequestParam(required = false) String activityName,
                                            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)LocalDateTime startTime,
                                            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)LocalDateTime endTime) {
        try {
            ActivityEntity activityEntity = activityService.queryActivityById(activityId);
            if (activityEntity == null) {
                log.error("更新活动{}失败，活动不存在", activityName);
                return Response.<Boolean>builder()
                       .code(ResponseCode.ACTIVITY_MANAGE_UPDATE_ACTIVITY_FAIL.getCode())
                       .info(ResponseCode.ACTIVITY_MANAGE_UPDATE_ACTIVITY_FAIL.getInfo())
                       .data(null)
                       .build();
            }
//            if (activityName != null) {
//                activityEntity.setActivityName(activityName);
//            }
//            if (startTime != null) {
//                activityEntity.setStartTime(startTime);
//            }
//            if (endTime!= null) {
//                activityEntity.setEndTime(endTime);
//            }
            if(!Objects.equals(activityName, "")){
                activityEntity.setActivityName(activityName);
            }
            if(!Objects.equals(startTime, "")){
                activityEntity.setStartTime(startTime);
            }
            if(!Objects.equals(endTime, "")){
                activityEntity.setEndTime(endTime);
            }
            boolean result = activityService.updateActivity(activityEntity);
            log.info("更新活动{}成功",activityName);
            return Response.<Boolean>builder()
                   .code(ResponseCode.SUCCESS.getCode())
                  .info(ResponseCode.SUCCESS.getInfo())
                  .data(result)
                  .build();
        }
        catch (Exception e) {
            log.error("更新活动{}，起始时间{}，截止时间{}，失败", activityName, startTime, endTime);
            return Response.<Boolean>builder()
                  .code(ResponseCode.ACTIVITY_MANAGE_UPDATE_ACTIVITY_FAIL.getCode())
                  .info(ResponseCode.ACTIVITY_MANAGE_UPDATE_ACTIVITY_FAIL.getInfo())
                  .data(null)
                 .build();
        }
    }

    @Override
    @Operation(description = "删除活动")
    @RequestMapping(value = "/delete-activity", method = RequestMethod.POST)
    public Response<Boolean> deleteActivity(@RequestParam Long activityId) {
        try {
            ActivityEntity activityEntity = activityService.queryActivityById(activityId);
            if (activityEntity == null) {
                log.error("删除活动{}失败，活动不存在", activityId);
                return Response.<Boolean>builder()
                      .code(ResponseCode.ACTIVITY_MANAGE_DELETE_ACTIVITY_FAIL.getCode())
                     .info(ResponseCode.ACTIVITY_MANAGE_DELETE_ACTIVITY_FAIL.getInfo())
                     .data(null)
                     .build();
            }
            boolean result = activityService.deleteActivity(activityId);
            log.info("删除活动{}成功",activityId);
            return Response.<Boolean>builder()
                 .code(ResponseCode.SUCCESS.getCode())
                 .info(ResponseCode.SUCCESS.getInfo())
                 .data(result)
                 .build();
        }
        catch (Exception e) {
            log.error("删除活动{}，失败", activityId);
            return Response.<Boolean>builder()
                .code(ResponseCode.ACTIVITY_MANAGE_DELETE_ACTIVITY_FAIL.getCode())
                .info(ResponseCode.ACTIVITY_MANAGE_DELETE_ACTIVITY_FAIL.getInfo())
                .data(null)
                .build();
        }
    }

    @Override
    @Operation(description = "根据活动名称模糊查询活动")
    @RequestMapping(value = "/query-activity-by-name", method = RequestMethod.GET)
    public Response<List<ActivityDTO>> queryActivityByName(@RequestParam String activityName) {
        try {
            List<ActivityEntity> activityEntities = activityService.queryActivityByName(activityName);
            if (activityEntities == null) {
                log.error("查询活动{}失败，活动不存在", activityName);
                return Response.<List<ActivityDTO>>builder()
                    .code(ResponseCode.ACTIVITY_MANAGE_QUERY_ACTIVITY_BY_NAME_FAIL.getCode())
                    .info(ResponseCode.ACTIVITY_MANAGE_QUERY_ACTIVITY_BY_NAME_FAIL.getInfo())
                    .data(null)
                    .build();
            }
            ArrayList<ActivityDTO> activityDTOS = new ArrayList<>();
            for (ActivityEntity activityEntity : activityEntities) {
                ActivityDTO activityDTO = ActivityDTO.builder()
                                .activityName(activityEntity.getActivityName())
                                .startTime(activityEntity.getStartTime())
                                .endTime(activityEntity.getEndTime())
                                .build();
                activityDTOS.add(activityDTO);
            }
            log.info("根据活动名称查询活动{}成功",activityName);
            return Response.<List<ActivityDTO>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(activityDTOS)
               .build();
        }
        catch (Exception e) {
            log.error("根据活动名称查询活动{}，失败", activityName);
            return Response.<List<ActivityDTO>>builder()
               .code(ResponseCode.ACTIVITY_MANAGE_QUERY_ACTIVITY_BY_NAME_FAIL.getCode())
               .info(ResponseCode.ACTIVITY_MANAGE_QUERY_ACTIVITY_BY_NAME_FAIL.getInfo())
               .data(null)
              .build();
        }
    }

    @Override
    @Operation(description = "根据活动id查询活动")
    @RequestMapping(value = "/query-activity-by-id", method = RequestMethod.GET)
    public Response<ActivityDTO> queryActivityById(@RequestParam Long activityId) {
        try {
            ActivityEntity activityEntity = activityService.queryActivityById(activityId);
            if (activityEntity == null) {
                log.error("根据活动id查询活动{}失败，活动不存在", activityId);
                return Response.<ActivityDTO>builder()
                   .code(ResponseCode.ACTIVITY_MANAGE_QUERY_ACTIVITY_BY_ID_FAIL.getCode())
                  .info(ResponseCode.ACTIVITY_MANAGE_QUERY_ACTIVITY_BY_ID_FAIL.getInfo())
                  .data(null)
                  .build();
            }
            ActivityDTO activityDTO = ActivityDTO.builder()
                            .activityName(activityEntity.getActivityName())
                            .startTime(activityEntity.getStartTime())
                            .endTime(activityEntity.getEndTime())
                            .build();
            log.info("根据活动id查询活动{}成功",activityId);
            return Response.<ActivityDTO>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(activityDTO)
                .build();
        }
        catch (Exception e) {
            log.error("根据活动id查询活动{}，失败", activityId);
            return Response.<ActivityDTO>builder()
              .code(ResponseCode.ACTIVITY_MANAGE_QUERY_ACTIVITY_BY_ID_FAIL.getCode())
              .info(ResponseCode.ACTIVITY_MANAGE_QUERY_ACTIVITY_BY_ID_FAIL.getInfo())
              .data(null)
              .build();
        }
    }

    @Override
    @Operation(description = "分页查询活动")
    @RequestMapping(value = "/query-activity-by-page", method = RequestMethod.GET)
    public Response<List<ActivityDTO>> queryActivityByPage(@RequestParam int pageNum,
                                                           @RequestParam int pageSize) {
        try {
            List<ActivityEntity> activityEntities = activityService.queryActivityByPage(pageNum, pageSize);
            if (activityEntities == null) {
                log.error("分页查询活动失败，活动不存在");
                return Response.<List<ActivityDTO>>builder()
                   .code(ResponseCode.ACTIVITY_MANAGE_QUERY_ACTIVITY_BY_PAGE_FAIL.getCode())
                  .info(ResponseCode.ACTIVITY_MANAGE_QUERY_ACTIVITY_BY_PAGE_FAIL.getInfo())
                  .data(null)
                  .build();
            }
            ArrayList<ActivityDTO> activityDTOS = new ArrayList<>();
            for (ActivityEntity activityEntity : activityEntities) {
                ActivityDTO activityDTO = ActivityDTO.builder()
                               .activityName(activityEntity.getActivityName())
                               .startTime(activityEntity.getStartTime())
                               .endTime(activityEntity.getEndTime())
                               .build();
                activityDTOS.add(activityDTO);
            }
            log.info("分页查询活动成功,起始数量为{},每页大小为{}",pageNum,pageSize);
            return Response.<List<ActivityDTO>>builder()
               .code(ResponseCode.SUCCESS.getCode())
               .info(ResponseCode.SUCCESS.getInfo())
               .data(activityDTOS)
               .build();
        }
        catch (Exception e) {
            log.error("分页查询活动失败,起始数量为{},每页大小为{}",pageNum,pageSize);
            return Response.<List<ActivityDTO>>builder()
             .code(ResponseCode.ACTIVITY_MANAGE_QUERY_ACTIVITY_BY_PAGE_FAIL.getCode())
             .info(ResponseCode.ACTIVITY_MANAGE_QUERY_ACTIVITY_BY_PAGE_FAIL.getInfo())
            .data(null)
            .build();
        }
    }

    @Override
    @RequestMapping(value = "/create-prize", method = RequestMethod.POST)
    @Operation(description = "创建奖品")
    public Response<Boolean> createPrize(
                                         @RequestParam String prizeName,
                                         @RequestParam String prizeDescription,
                                         @RequestParam String prizeType,
                                         @RequestParam int stockAvailable,
                                         @RequestParam double probability) {
        try {
            PrizeEntity prizeEntity = new PrizeEntity();
            prizeEntity.setPrizeName(prizeName);
            prizeEntity.setPrizeDescription(prizeDescription);
            prizeEntity.setPrizeType(prizeType);
            prizeEntity.setStockAvailable(stockAvailable);
            prizeEntity.setStockUsed(0);
            prizeEntity.setProbability(probability);


            prizeService.createPrize(prizeEntity);


            log.info("创建奖品{}成功",prizeName);
            return Response.<Boolean>builder()
               .code(ResponseCode.SUCCESS.getCode())
               .info(ResponseCode.SUCCESS.getInfo())
               .data(true)
               .build();
        }
        catch (Exception e) {
            log.error("创建奖品{}，失败", prizeName);
            return Response.<Boolean>builder()
             .code(ResponseCode.PRIZE_MANAGE_CREATE_PRIZE_FAIL.getCode())
             .info(ResponseCode.PRIZE_MANAGE_CREATE_PRIZE_FAIL.getInfo())
             .data(false)
            .build();
        }
    }

    @Override
    @RequestMapping(value = "/delete-prize", method = RequestMethod.POST)
    @Operation(description = "删除奖品")
    public Response<Boolean> deletePrize(@RequestParam Long prizeId) {
        try {
            prizeService.deletePrize(prizeId);
            log.info("删除奖品{}成功",prizeId);
            return Response.<Boolean>builder()
             .code(ResponseCode.SUCCESS.getCode())
             .info(ResponseCode.SUCCESS.getInfo())
             .data(true)
             .build();
        }
        catch (Exception e) {
            log.error("删除奖品{}，失败", prizeId);
            return Response.<Boolean>builder()
            .code(ResponseCode.PRIZE_MANAGE_DELETE_PRIZE_FAIL.getCode())
            .info(ResponseCode.PRIZE_MANAGE_DELETE_PRIZE_FAIL.getInfo())
            .data(false)
           .build();
        }
    }

    @Override
    @RequestMapping(value = "/update-prize", method = RequestMethod.POST)
    @Operation(description = "更新奖品")
    public Response<Boolean> updatePrize(@RequestParam Long prizeId,
                                         @RequestParam(required = false) String prizeName,
                                         @RequestParam(required = false) String prizeDescription,
                                         @RequestParam(required = false) String prizeType,
                                         @RequestParam(required = false) double probability) {

        try {
            PrizeEntity prizeEntity = prizeService.queryPrizeById(prizeId);
            if (prizeEntity == null) {
                log.error("更新奖品{}失败，奖品不存在", prizeId);
                return Response.<Boolean>builder()
                    .code(ResponseCode.PRIZE_MANAGE_UPDATE_PRIZE_FAIL.getCode())
                    .info(ResponseCode.PRIZE_MANAGE_UPDATE_PRIZE_FAIL.getInfo())
                    .data(false)
                    .build();
            }
            if (!Objects.equals(prizeName, "")) {
                prizeEntity.setPrizeName(prizeName);
            }
            if (!Objects.equals(prizeDescription, "")) {
                prizeEntity.setPrizeDescription(prizeDescription);
            }
            if (!Objects.equals(prizeType, "")) {
                prizeEntity.setPrizeType(prizeType);
            }
            if (probability!= prizeEntity.getProbability()) {
                prizeEntity.setProbability(probability);
            }

            prizeService.updatePrize(prizeEntity);
            log.info("更新奖品{}成功",prizeId);
            return Response.<Boolean>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(true)
                    .build();
        }
        catch (Exception e) {
            log.error("更新奖品{}，失败", prizeId);
            return Response.<Boolean>builder()
           .code(ResponseCode.PRIZE_MANAGE_UPDATE_PRIZE_FAIL.getCode())
           .info(ResponseCode.PRIZE_MANAGE_UPDATE_PRIZE_FAIL.getInfo())
           .data(false)
           .build();
        }

    }

    @Override
    @RequestMapping(value = "/query-prize-by-name", method = RequestMethod.GET)
    @Operation(description = "根据奖品名称模糊查询奖品")
    public Response<List<PrizeDTO>> queryPrizeByName(@RequestParam String prizeName) {
        try {
            List<PrizeEntity> prizeEntities = prizeService.queryPrizeByName(prizeName);
            if (prizeEntities == null) {
                log.error("查询奖品{}失败，奖品不存在", prizeName);
                return Response.<List<PrizeDTO>>builder()
                  .code(ResponseCode.PRIZE_MANAGE_QUERY_PRIZE_BY_NAME_FAIL.getCode())
                  .info(ResponseCode.PRIZE_MANAGE_QUERY_PRIZE_BY_NAME_FAIL.getInfo())
                  .data(null)
                 .build();
            }
            ArrayList<PrizeDTO> prizeDTOS = new ArrayList<>();
            for (PrizeEntity prizeEntity : prizeEntities) {
                PrizeDTO prizeDTO = PrizeDTO.builder()
                                .prizeId(prizeEntity.getPrizeId())
                                .prizeName(prizeEntity.getPrizeName())
                                .prizeDescription(prizeEntity.getPrizeDescription())
                                .prizeType(prizeEntity.getPrizeType())
                                .probability(prizeEntity.getProbability())
                                .stockAvailable(prizeEntity.getStockAvailable())
                                .stockUsed(prizeEntity.getStockUsed())
                                .build();
                prizeDTOS.add(prizeDTO);
            }
            log.info("查询奖品{}成功",prizeName);
            return Response.<List<PrizeDTO>>builder()
            .code(ResponseCode.SUCCESS.getCode())
            .info(ResponseCode.SUCCESS.getInfo())
            .data(prizeDTOS)
            .build();

        }
        catch (Exception e) {
            log.error("查询奖品{}，失败", prizeName);
            return Response.<List<PrizeDTO>>builder()
            .code(ResponseCode.PRIZE_MANAGE_QUERY_PRIZE_BY_NAME_FAIL.getCode())
            .info(ResponseCode.PRIZE_MANAGE_QUERY_PRIZE_BY_NAME_FAIL.getInfo())
            .data(null)
            .build();
        }

    }

    @Override
    @RequestMapping(value = "/query-prize-by-id", method = RequestMethod.GET)
    @Operation(description = "根据奖品id查询奖品")
    public Response<PrizeDTO> queryPrizeById(@RequestParam Long prizeId) {
        try {
            PrizeEntity prizeEntity = prizeService.queryPrizeById(prizeId);
            if (prizeEntity == null) {
                log.error("查询奖品{}失败，奖品不存在", prizeId);
                return Response.<PrizeDTO>builder()
                  .code(ResponseCode.PRIZE_MANAGE_QUERY_PRIZE_BY_ID_FAIL.getCode())
                 .info(ResponseCode.PRIZE_MANAGE_QUERY_PRIZE_BY_ID_FAIL.getInfo())
                 .data(null)
                 .build();
            }
            PrizeDTO prizeDTO = PrizeDTO.builder()
                        .prizeId(prizeEntity.getPrizeId())
                        .prizeName(prizeEntity.getPrizeName())
                        .prizeDescription(prizeEntity.getPrizeDescription())
                        .prizeType(prizeEntity.getPrizeType())
                        .probability(prizeEntity.getProbability())
                        .stockAvailable(prizeEntity.getStockAvailable())
                        .stockUsed(prizeEntity.getStockUsed())
                        .build();
            log.info("查询奖品{}成功",prizeId);
            return Response.<PrizeDTO>builder()
            .code(ResponseCode.SUCCESS.getCode())
            .info(ResponseCode.SUCCESS.getInfo())
            .data(prizeDTO)
            .build();
        }
        catch (Exception e) {
            log.error("查询奖品{}，失败", prizeId);
            return Response.<PrizeDTO>builder()
           .code(ResponseCode.PRIZE_MANAGE_QUERY_PRIZE_BY_ID_FAIL.getCode())
           .info(ResponseCode.PRIZE_MANAGE_QUERY_PRIZE_BY_ID_FAIL.getInfo())
           .data(null)
           .build();
        }

    }

    @Override
    @RequestMapping(value = "/query-prize-by-page", method = RequestMethod.GET)
    @Operation(description = "分页查询奖品")
    public Response<List<PrizeDTO>> queryPrizeByPage(@RequestParam int pageNum,
                                                     @RequestParam int pageSize) {
        try {
            List<PrizeEntity> prizeEntities = prizeService.queryPrizeByPage(pageNum, pageSize);
            if (prizeEntities == null) {
                log.error("分页查询奖品失败，奖品不存在");
                return Response.<List<PrizeDTO>>builder()
                 .code(ResponseCode.PRIZE_MANAGE_QUERY_PRIZE_BY_PAGE_FAIL.getCode())
                .info(ResponseCode.PRIZE_MANAGE_QUERY_PRIZE_BY_PAGE_FAIL.getInfo())
                .data(null)
                .build();
            }
            ArrayList<PrizeDTO> prizeDTOS = new ArrayList<>();
            for (PrizeEntity prizeEntity : prizeEntities) {
                PrizeDTO prizeDTO = PrizeDTO.builder()
                                .prizeId(prizeEntity.getPrizeId())
                                .prizeName(prizeEntity.getPrizeName())
                                .prizeDescription(prizeEntity.getPrizeDescription())
                                .prizeType(prizeEntity.getPrizeType())
                                .probability(prizeEntity.getProbability())
                                .stockAvailable(prizeEntity.getStockAvailable())
                                .stockUsed(prizeEntity.getStockUsed())
                                .build();
                prizeDTOS.add(prizeDTO);
            }
            log.info("分页查询奖品成功,起始数量为{},每页大小为{}",pageNum,pageSize);
            return Response.<List<PrizeDTO>>builder()
            .code(ResponseCode.SUCCESS.getCode())
            .info(ResponseCode.SUCCESS.getInfo())
            .data(prizeDTOS)
            .build();

        }
        catch (Exception e) {
            log.error("分页查询奖品失败,起始数量为{},每页大小为{}",pageNum,pageSize);
            return Response.<List<PrizeDTO>>builder()
           .code(ResponseCode.PRIZE_MANAGE_QUERY_PRIZE_BY_PAGE_FAIL.getCode())
           .info(ResponseCode.PRIZE_MANAGE_QUERY_PRIZE_BY_PAGE_FAIL.getInfo())
          .data(null)
          .build();

        }

    }

    @Override
    @RequestMapping(value = "/add-prize-stock", method = RequestMethod.POST)
    @Operation(description = "增加奖品库存")
    public Response<Boolean> addPrizeStock(@RequestParam Long prizeId,
                                           @RequestParam int stock) {
        try{
            prizeService.updatePrizeStock(prizeId, stock);
            log.info("增加奖品{}:{}库存成功",prizeId,stock);
            return Response.<Boolean>builder()
            .code(ResponseCode.SUCCESS.getCode())
           .info(ResponseCode.SUCCESS.getInfo())
           .data(true)
           .build();
        }
        catch (Exception e) {
            log.error("增加奖品{}:{}库存失败",prizeId,stock);
            return Response.<Boolean>builder()
          .code(ResponseCode.PRIZE_MANAGE_ADD_PRIZE_STOCK_FAIL.getCode())
          .info(ResponseCode.PRIZE_MANAGE_ADD_PRIZE_STOCK_FAIL.getInfo())
          .data(false)
          .build();
        }

    }

    @Override
    @RequestMapping(value = "/update-strategy", method = RequestMethod.POST)
    @Operation(description = "更新策略")
    public Response<Boolean> updateStrategy(@RequestParam Long activityId,
                                            @RequestParam String strategyName,
                                            @RequestParam(required = false)String prizeIds,
                                            @RequestParam(required = false)String config) {
        try {
            ActivityStrategyPrizeEntity activityStrategyPrizeEntity = activityStrategyPrizeService.queryStrategy(activityId, strategyName);
            if (activityStrategyPrizeEntity == null) {
                log.error("更新策略{}失败，策略不存在", strategyName);
                return Response.<Boolean>builder()
                .code(ResponseCode.ACTIVITY_STRATEGY_PRIZE_UPDATE_STRATEGY_FAIL.getCode())
               .info(ResponseCode.ACTIVITY_STRATEGY_PRIZE_UPDATE_STRATEGY_FAIL.getInfo())
               .data(false)
               .build();
            }
            activityStrategyPrizeEntity.setPrizeIds(prizeIds);
            activityStrategyPrizeEntity.setConfig(config);
            activityStrategyPrizeService.updateStrategy(activityStrategyPrizeEntity);
            log.info("更新策略{}成功",strategyName);
            return Response.<Boolean>builder()
             .code(ResponseCode.SUCCESS.getCode())
             .info(ResponseCode.SUCCESS.getInfo())
             .data(true)
             .build();
        } catch (Exception e) {
            log.error("更新策略{}失败", strategyName);
            return Response.<Boolean>builder()
           .code(ResponseCode.ACTIVITY_STRATEGY_PRIZE_UPDATE_STRATEGY_FAIL.getCode())
           .info(ResponseCode.ACTIVITY_STRATEGY_PRIZE_UPDATE_STRATEGY_FAIL.getInfo())
           .data(false)
           .build();
        }

    }

    @Override
    @RequestMapping(value = "/query-strategy", method = RequestMethod.GET)
    @Operation(description = "根据活动id和策略名称查询策略信息")
    public Response<ActivityStrategyPrizeDTO> queryStrategy(@RequestParam Long activityId, @RequestParam String strategyName) {
        try {
            ActivityStrategyPrizeEntity activityStrategyPrizeEntity = activityStrategyPrizeService.queryStrategy(activityId, strategyName);
            if (activityStrategyPrizeEntity == null) {
                log.error("查询策略{}失败，策略不存在", strategyName);
                return Response.<ActivityStrategyPrizeDTO>builder()
               .code(ResponseCode.ACTIVITY_STRATEGY_PRIZE_QUERY_STRATEGY_FAIL.getCode())
               .info(ResponseCode.ACTIVITY_STRATEGY_PRIZE_QUERY_STRATEGY_FAIL.getInfo())
               .data(null)
               .build();
            }
            ActivityStrategyPrizeDTO activityStrategyPrizeDTO = ActivityStrategyPrizeDTO.builder()
                        .activityId(activityStrategyPrizeEntity.getActivityId())
                        .strategyName(activityStrategyPrizeEntity.getStrategyName())
                        .prizeIds(activityStrategyPrizeEntity.getPrizeIds())
                        .config(activityStrategyPrizeEntity.getConfig())
                        .build();
            log.info("查询策略{}成功",strategyName);
            return Response.<ActivityStrategyPrizeDTO>builder()
            .code(ResponseCode.SUCCESS.getCode())
            .info(ResponseCode.SUCCESS.getInfo())
            .data(activityStrategyPrizeDTO)
            .build();

        }
        catch (Exception e) {
            log.error("查询策略{}失败", strategyName);
            return Response.<ActivityStrategyPrizeDTO>builder()
           .code(ResponseCode.ACTIVITY_STRATEGY_PRIZE_QUERY_STRATEGY_FAIL.getCode())
           .info(ResponseCode.ACTIVITY_STRATEGY_PRIZE_QUERY_STRATEGY_FAIL.getInfo())
           .data(null)
           .build();
        }
    }

    @Override
    @RequestMapping(value = "/query-strategy-by-activity-id", method = RequestMethod.GET)
    @Operation(description = "根据活动id查询所有的策略信息")
    public Response<List<ActivityStrategyPrizeDTO>> queryStrategyByActivityId(@RequestParam Long activityId) {
        try {
            List<ActivityStrategyPrizeEntity> activityStrategyPrizeEntities = activityStrategyPrizeService.queryStrategyByActivityId(activityId);
            if (activityStrategyPrizeEntities == null) {
                log.error("查询活动{}策略失败，策略不存在", activityId);
                return Response.<List<ActivityStrategyPrizeDTO>>builder()
              .code(ResponseCode.ACTIVITY_STRATEGY_PRIZE_QUERY_STRATEGY_BY_ACTIVITY_ID_FAIL.getCode())
              .info(ResponseCode.ACTIVITY_STRATEGY_PRIZE_QUERY_STRATEGY_BY_ACTIVITY_ID_FAIL.getInfo())
              .data(null)
              .build();
            }
            ArrayList<ActivityStrategyPrizeDTO> activityStrategyPrizeDTOS = new ArrayList<>();
            for (ActivityStrategyPrizeEntity activityStrategyPrizeEntity : activityStrategyPrizeEntities) {
                ActivityStrategyPrizeDTO activityStrategyPrizeDTO = ActivityStrategyPrizeDTO.builder()
                                .activityId(activityStrategyPrizeEntity.getActivityId())
                                .strategyName(activityStrategyPrizeEntity.getStrategyName())
                                .prizeIds(activityStrategyPrizeEntity.getPrizeIds())
                                .config(activityStrategyPrizeEntity.getConfig())
                                .build();
                activityStrategyPrizeDTOS.add(activityStrategyPrizeDTO);
            }
            log.info("查询活动{}策略成功",activityId);
            return Response.<List<ActivityStrategyPrizeDTO>>builder()
           .code(ResponseCode.SUCCESS.getCode())
           .info(ResponseCode.SUCCESS.getInfo())
           .data(activityStrategyPrizeDTOS)
           .build();
        }
        catch (Exception e) {
            log.error("查询活动{}策略失败", activityId);
            return Response.<List<ActivityStrategyPrizeDTO>>builder()
           .code(ResponseCode.ACTIVITY_STRATEGY_PRIZE_QUERY_STRATEGY_BY_ACTIVITY_ID_FAIL.getCode())
          .info(ResponseCode.ACTIVITY_STRATEGY_PRIZE_QUERY_STRATEGY_BY_ACTIVITY_ID_FAIL.getInfo())
          .data(null)
          .build();

        }
    }

    @Override
    @RequestMapping(value = "/preload-by-activity-id", method = RequestMethod.GET)
    @Operation(description = "根据活动id进行活动预热")
    public Response<Boolean> preload(Long activityId) {
        try {
            preloadService.preload(activityId);
            log.info("活动{}预热成功",activityId);
            return Response.<Boolean>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(true)
                    .build();
        }
        catch (Exception e) {
            log.error("活动{}预热失败", activityId);
            return Response.<Boolean>builder()
             .code(ResponseCode.PRELOAD_PRELOAD_FAIL.getCode())
             .info(ResponseCode.PRELOAD_PRELOAD_FAIL.getInfo())
             .data(false)
            .build();
        }
    }
}
